package Well_Game;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.text.Text;

public class Well_Game_version3 extends Application {
    private int player = 1;
    private int step = 0;
    private BorderPane pane = new BorderPane();
    private Cell cell = new Cell();
    private Text text = new Text("X's turn");
    private boolean canrestart = false;
    @Override
    public void start(Stage stage){
        Button new_game_button = new Button("New Game");
        StackPane toppane = new StackPane(new_game_button);
        Pane bottompane = new Pane(text);

        pane.setCenter(cell);
        pane.setTop(toppane);
        pane.setBottom(bottompane);

        NewGame newGame = new NewGame();
        new_game_button.setOnAction(newGame);
        for (int i = 0; i <9 ; i++) {
            int finalI = i;
            cell.list.get(i).setOnMouseClicked(event -> setchessman(finalI));
        }

        Scene scene = new Scene(pane, 300, 370);
        stage.setTitle("version3");
        stage.setScene(scene);
        stage.show();
    }
    class NewGame implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent event){
            cell.initialize();
            cell.getChildren().clear();
            cell.reloadrectangle();
            if(canrestart) {
                player = 1;
                step = 0;
                text.setText(text.getText() + ".Restart the game");
                canrestart = false;
            }
        }
    }
    private void setchessman(int no) {
        int i = no / 3;
        int j = no % 3;
        canrestart = true;
        if(player != 0) {
            if (cell.getWell(i, j) == 0) {
                cell.setWell(i, j, player);
                if (player == 1) {
                    cell.printX(no);
                    player++;
                    step++;
                } else {
                    cell.printO(no);
                    player--;
                    step++;
                }
            }
            switch (judge(step)) {
                case 0: {
                    if (player == 1) {
                        text.setText("X's turn");
                    } else {
                        text.setText("O's turn");
                    }
                    break;
                }
                case 1: {
                    text.setText("X won!The game is over");
                    player = 0;
                    break;
                }
                case 2: {
                    text.setText("O won!The game is over");
                    player = 0;
                    break;
                }
                case 3: {
                    text.setText("Draw!The game is over");
                    player = 0;
                    break;
                }
            }
        }
    }
    private int judge(int step){
        for (int i = 0; i < 3; i++) {
            if(cell.getWell(i,0) == 1 && cell.getWell(i,1) == 1 && cell.getWell(i,2) == 1) {
                return 1;
            }
            else if(cell.getWell(i,0) == 2 && cell.getWell(i,1) == 2 && cell.getWell(i,2) == 2) {
                return 2;
            }
        }
        for (int j = 0; j < 3; j++) {
            if(cell.getWell(0,j) == 1 && cell.getWell(1,j) == 1 && cell.getWell(2,j) == 1) {
                return 1;
            }
            else if(cell.getWell(0,j) == 2 && cell.getWell(1,j) == 2 && cell.getWell(2,j) == 2) {
                return 2;
            }
        }
        if(cell.getWell(1,1) != 0){
            if(cell.getWell(0,0) == 1 && cell.getWell(1,1) == 1 && cell.getWell(2,2) == 1 ||
                    cell.getWell(0,2) == 1 && cell.getWell(1,1) == 1 && cell.getWell(2,0) == 1 ) {
                return 1;
            }
            else if(cell.getWell(0,0) == 2 && cell.getWell(1,1) == 2 && cell.getWell(2,2) == 2 ||
                    cell.getWell(0,2) == 2 && cell.getWell(1,1) == 2 && cell.getWell(2,0) == 2 ) {
                return 2;
            }
        }
        if(step == 9) {
            return 3;
        }
        return 0;
    }
}
